from selenium.webdriver.common.by import By
from pages.base_page import Page


class InvitationPage(Page):
    ele_title = (By.ID, u'page-autoInvitation-title')
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    ele_auto_invitations = (By.XPATH, u'//span[text()="Auto Invitations"]')
    ele_manual_invitations = (By.XPATH, u'//span[text()="Manual Invitation"]')
    ele_new_auto_invitation = (By.ID, u'table-autoInvitation-button-new')

    # crate a new invitation page
    ele_new_auto_invitation_name = (By.ID, u'form-autoInvitation-input-name')
    ele_new_auto_invitation_enable = (By.XPATH, u'//*[@id="form-autoInvitation"]//input[@name="isEnabled"]')
    ele_new_auto_invitation_save = (By.XPATH, u'(//button[@data-tag="formButton"])[1]')

    # delete the auto invitation we crated
    ele_delete_new_invitation = (
        By.XPATH, u'//tr/td/div/a[text()="test"]/../../../td[@data-tag="operation"]/div/button[3]')
    ele_delete_confirm_button = (By.XPATH, u'//button/span[text()="Delete"]')

    # 如果没有invitation功能，检查图片和chat with us 按钮
    ele_title_no_invitation_feature = (By.ID, u'autoInvitation-title')
    ele_img_no_auto_invitation_feature = (By.XPATH, u'//img[@alt="Auto Invitation Introduction"]')
    ele_chat_with_us = (By.XPATH, u'//span[text()="Chat with us"]')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def invitation_page_eles_check_when_has_invitation_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_auto_invitations).is_enabled()
        self.find_element(*self.ele_manual_invitations).is_enabled()
        self.find_element(*self.ele_new_auto_invitation).is_enabled()

    def invitation_page_eles_check_when_no_invitation_feature(self):
        self.find_element(*self.ele_title_no_invitation_feature).is_enabled()
        self.find_element(*self.ele_img_no_auto_invitation_feature).is_enabled()
        self.find_element(*self.ele_chat_with_us).is_enabled()

    def new_auto_invitation(self):
        self.click(self.ele_auto_invitations)
        self.click(self.ele_new_auto_invitation)
        self.input_text(self.ele_new_auto_invitation_name, "test")
        self.click(self.ele_new_auto_invitation_enable)
        self.click(self.ele_new_auto_invitation_save)

    def delete_auto_inviatation_just_created(self):
        """
        注意调用顺序，只有在新建auto invitation之后，才可以删除
        所以要先调用新建的方法
        :return:
        """
        self.click(self.ele_delete_new_invitation)
        # ele_alert = self.driver.switch_to.alert
        # ele_alert.accept()
        self.click(self.ele_delete_confirm_button)
